﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WebApi.Service;
using WebApi.Service.Dto;
using WebApi.Web.Models;
using WebApi.Service.Other;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebApi.Web.Controllers
{
    /// <summary>
    /// 标签相关的Controller
    /// </summary>
    [Route("api/[controller]")]
    public class TagController : Controller
    {
        private readonly ITagService tagService;
        /// <summary>
        /// TagController的构造函数
        /// </summary>
        /// <param name="tagService">注入的tagService</param>
        public TagController(ITagService tagService)
        {
            this.tagService = tagService;
        }
        /// <summary>
        /// 新增标签
        /// </summary>
        /// <param name="name">标签的名称</param>
        /// <returns></returns>
        [HttpPost]
        public ResponseResult<bool> AddNewTag(string name)
        {
            return tagService.AddNewTag(name);
        }
        /// <summary>
        /// 删除标签
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public ResponseResult<bool> DeleteTag(List<long>tagsId)
        {
            return tagService.DeleteTag(tagsId);
        }
        /// <summary>
        /// 更新标签
        /// </summary>
        /// <param name="tag">传入的标签更新的model</param>
        /// <returns>是否成功修改</returns>
        [HttpPut]
        public ResponseResult<bool> UpdateTag(TagModel tag)
        {
            return tagService.UpdateTag(new TagDto
            {
                Id = tag.Id,
                Name = tag.Name
            });
        }
        /// <summary>
        /// 获取未被删除、不分页的标签列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ResponseResult<List<TagModel>> GetTags()
        {
            var res = tagService.GetTags();
            var data=res.Data.Select(o => new TagModel
            {
                Id = o.Id,
                Name=o.Name
            }).ToList();
            return new ResponseResult<List<TagModel>>()
            {
                State = res.State,
                Message = res.Message,
                Data = data
            };
        }
    }
}
